package main.java.easy;

import java.util.Arrays;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-01-13:16
 * @Description: 种花，贪心算法
 */
public class PlaceFlowers {
    public static boolean canPlaceFlowers(int[] flowerbed,int n){
        int len = flowerbed.length;
        if (len==0){
            return false;
        }
        if (len==1){
            return flowerbed[0]==0&&n<=1 || n==0;
        }
        if (len==2){
            return flowerbed[0]==0&&flowerbed[1]==0&&n<=1 || n==0;
        }
        if (len==3){
            return (flowerbed[1]==0 && flowerbed[0]==0 && flowerbed[2]==0 && n<=2)||(flowerbed[1]==0 && (flowerbed[0]==0 || flowerbed[2]==0) && n<=1);
        }
        int index=0;
        if (flowerbed[index]==0&&flowerbed[index+1]==0){
            flowerbed[index]=1;
            n--;
        }
        index+=2;
        while (index<=len-2){//倒数第二位
            if (flowerbed[index]==0&&flowerbed[index-1]==0&&flowerbed[index+1]==0){
                flowerbed[index]=1;
                n--;
            }
            index++;
        }
        System.out.println(index);
        if (index==len-1&&flowerbed[index]==0&&flowerbed[index-1]==0){
            flowerbed[index]=1;
            n--;
        }

        return n <= 0;
    }

    public static void main(String[] args) {
        int[] ints = new int[]{1,0,0,0};
        System.out.println(canPlaceFlowers(ints, 1));
        System.out.println(Arrays.toString(ints));
    }
}
